// pages/index/component/groupService/groupService.js
import {
  list
} from '../../../../api/bargainBuy'
const util = require('../../../../utils/util')
import {
  list as pageList
} from '../../../../api/wxPage'
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pages: [],
    tagIds: [],
    pageSize:20,
    products: []
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached() {
      if(this.data.info.rows!=''||this.data.info.rows!=null){
        this.data.pageSize=this.data.info.rows
        this.setData({
          pageSize:this.data.pageSize
        })
      }

      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })
      this.loadPage()
      this.loadProduct()
    }
  },

  methods: {
    toPage(e) {
      wx.navigateTo({
              url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id
      })
},
    loadPage() {
      var _this = this;
      pageList({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds,
      }).then(res => {
        _this.data.pages = res.data.data
        this.setData({
          pages: this.data.pages
        })
      })
    },
    toProduct(data) {
      wx.navigateTo({
        url: '/pages/goods/bargain/barginDetails?id=' + data.currentTarget.dataset.id
      })
    },
    loadProduct() {
      list({
        mchId: app.globalData.mchId,
      }).then(res => {
        res.data.data.forEach(item => {
          item.meta = JSON.parse(item.meta)
          item.point=(item.point/100).toFixed(2)
          item.thumbnail = util.thumbnail(item.thumbnail, 230, 230)
        })
        this.data.products = res.data.data
        
        this.setData({
          products: this.data.products
        })
      })
    },
  }
})